import type { RouteRecordRaw } from 'vue-router'
import { createRouter, createWebHashHistory } from 'vue-router'
import message from '@/components/common/message'
import { getToken } from '../request/index'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Main',
    component: () => import('../views/Main.vue'),
    children: [
      {
        path: '',
        name: 'Home',
        component: () => import('../views/HomePage.vue'),
      },
      {
        path: 'bank',
        name: 'Bank',
        component: () => import('../views/bank/BankLayout.vue'),
        children: [
          {
            path: '',
            name: 'Manage',
            component: () => import('../views/bank/ManagePage.vue'),
            children: [{
              path: '',
              name: 'InClass',
              component: () => import('../views/bank/manage/InClass.vue'),
            }, {
              path: 'outclass',
              name: 'OutClass',
              component: () => import('../views/bank/manage/OutClass.vue'),
            }],
          },
          {
            // 新建题组
            path: 'create',
            name: 'CreateQuestionGroup',
            component: () => import('../views/bank/CreatePage.vue'),
            children: [{
              // 阅读题目
              path: '',
              name: 'ReadingQuestionGroup',
              component: () => import('../views/bank/create/ReadingPage.vue'),
            }, {
              // 预习题目
              path: 'preview',
              name: 'PreviewQuestionGroup',
              component: () => import('../views/bank/create/PreviewPage.vue'),
            }],
          },
          {
            // 编辑题组
            path: 'edit',
            name: 'EditQuestionGroup',
            component: () => import('../views/bank/EditPage.vue'),
            children: [{
              path: ':id?',
              name: 'QuestionGroupEditor',
              component: () => import('../views/bank/edit/Editor.vue'),
            }],
          },
        ],
      },
      {
        path: 'homework',
        name: 'Homework',
        component: () => import('../views/homework/HomeworkLayout.vue'),
        children: [{
          path: '',
          name: 'HomeworkManage',
          component: () => import('../views/homework/HomeworkPage.vue'),
        }, {
          path: 'detail/:classId/:id',
          name: 'HomeworkDetail',
          component: () => import('../views/homework/HomeworkDetail.vue'),
        }, {
          path: 'edit/:homeworkId?',
          name: 'HomeworkEdit',
          component: () => import('../views/homework/HomeworkEditor.vue'),
        }],
      },
      {
        path: 'class',
        name: 'Class',
        component: () => import('../views/ClassPage.vue'),
        children: [
          {
            path: '',
            name: 'HomeContent',
            component: () => import('../views/class/ClassContent.vue'),
          },
          {
            path: 'create-class',
            name: 'CreateClass',
            component: () => import('../views/class/CreateClassPage.vue'),
          },
          {
            path: 'create-class/:cid',
            name: 'EditClass',
            component: () => import('../views/class/CreateClassPage.vue'),
          },
        ],
      },
      {
        path: 'data',
        name: 'Data',
        component: () => import('../views/DataPage.vue'),
      },
      {
        path: 'settings',
        name: 'Settings',
        component: () => import('../views/SettingsPage.vue'),
      },
    ],
  },
  {
    path: '/auth',
    name: 'Auth',
    component: () => import('../views/Auth.vue'),
    children: [
      {
        path: '',
        name: 'Login',
        component: () => import('../views/auth/Login.vue'),
      },
      {
        path: 'register',
        name: 'Register',
        component: () => import('../views/auth/Register.vue'),
      },
      {
        path: 'verify-login',
        name: 'VerifyLogin',
        component: () => import('../views/auth/VerifyLogin.vue'),
      },
    ],
    meta: {
      ignoreAuth: true,
    },
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

// 路由拦截器：鉴权
router.beforeEach((to, _from, next) => {
  // 如果路由设置了 ignoreAuth，则直接放行
  if ((import.meta.env.DEV && import.meta.env.VITE_IGNORE_AUTH) || to.meta?.ignoreAuth) {
    return next()
  }
  const token = getToken() ?? localStorage.getItem('token')
  if (!token) {
    // 未登录，跳转到登录页
    message.info('请先登录')
    return next({ path: '/auth' })
  }
  // 已登录访问 /auth 相关页面，自动跳转到首页
  if (to.path.startsWith('/auth')) {
    return next({ path: '/' })
  }
  next()
})

export default router
